package com.xyx.ssm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xyx.ssm.po.Purchase;
import com.xyx.ssm.po.Purchasesub;
import com.xyx.ssm.po.WineBrand;
import com.xyx.ssm.service.PurchaseService;
import com.xyx.ssm.util.Page;

@Controller
@RequestMapping(value = "/purchaseAction")
public class PurchaseAction {
	@Resource
	@Autowired
	private PurchaseService purchaseService;

	/**
	 * 页面入口
	 * 
	 * @return
	 */
	@RequestMapping("/purchaseshouye")
	public String purchase() {
		return "role/purchase";
	}

	@RequestMapping("/jsons")
	@ResponseBody
	public Map<String, Object> jsons(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart, String pageName,
			String departName) {

		Map<String, Object> map111 = new HashMap<String, Object>();
		// 用户名
		map111.put("pageName", "");
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		System.out.println(map111.toString());
		// 1:当前页码
		// 1:查询参数
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, map111, iDisplayLength);
		Page<Map<String, Object>> page1 = purchaseService.findPage(page);
		int num = page1.getList().size();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ceshi> list = new ArrayList<Ceshi>();
		for (int i = 0; i < num; i++) {
			Ceshi ceshi = new Ceshi();
			ceshi.setId(i);
			ceshi.setUsername(page1.getList().get(i).get("id").toString());
			list.add(ceshi);
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}

	@RequestMapping("/insertpruchase")
	@ResponseBody
	public Map<String, Object> insertpruchase(Purchase purchase) {
		int num = purchaseService.insertpruchase(purchase);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "添加成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "添加失败！");
		}
		return mapjson;
	}

	@RequestMapping("/selectwineproduct")
	@ResponseBody
	public Map<String, Object> selectWineproduct(Purchase purchase,
			HttpSession session) {
		List<Map<String, Object>> list = purchaseService
				.selectWineproduct(purchase);
		session.setAttribute("list", list);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		mapjson.put("id", purchase.getId());
		return mapjson;

	}

	@RequestMapping("/selectPurchaseDetail")
	@ResponseBody
	public Map<String, Object> selectdetail(Integer id, HttpSession session) {
		Purchase purchase = new Purchase();
		purchase.setId(id);
		List<Purchase> list = purchaseService.selectdetail(purchase);
		List<Purchase> list1 = purchaseService.selectdetailwine(purchase);
		System.out.println("-------------------------------:" + list);
		System.out.println(list1);
		if (list.size() > 0 && list1.size() > 0) {
			Map<String, Object> mapjson = new HashMap<String, Object>();
			mapjson.put("name", list.get(0).getName());
			mapjson.put("starttime", list.get(0).getStarttime());
			mapjson.put("endtime", list.get(0).getEndtime());
			mapjson.put("detail", list.get(0).getDetail());
			mapjson.put("state", list.get(0).getState());
			session.setAttribute("list1", list1);
			return mapjson;
		} else {
			Map<String, Object> mapjson = new HashMap<String, Object>();
			mapjson.put("flag", false);
			mapjson.put("msg", "请添加产品！");
			return mapjson;
		}

	}
	
	/**
	 * 订单管理查询
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("/Purchaseupdate")
	@ResponseBody
	public Map<String, Object> selectPurchaseupdate(Integer id) {
		Purchase purchase = new Purchase();
		purchase.setId(id);
		List<Purchase> list = purchaseService.selectdetail(purchase);
		System.out.println("-------------------------------:" + list);
		
			Map<String, Object> mapjson = new HashMap<String, Object>();
			mapjson.put("id", list.get(0).getId());
			mapjson.put("name", list.get(0).getName());
			mapjson.put("starttime", list.get(0).getStarttime());
			mapjson.put("endtime", list.get(0).getEndtime());
			mapjson.put("detail", list.get(0).getDetail());
			mapjson.put("state", list.get(0).getState());
			return mapjson;

	}
	
	
	
	@RequestMapping("/insertwinepurchasesub")
	@ResponseBody
	public Map<String, Object> insertwinepurchasesub(String phid) {
		Purchasesub purchasesub=new Purchasesub();
		String [] phid1=phid.split(",");
		int phid11=Integer.parseInt(phid1[0]);
		purchasesub.setPhid(phid11);
		int num=1;
		for(int i=1;i<phid1.length;i++){
			int wid11=Integer.parseInt(phid1[i]);
				purchasesub.setWid(wid11);
				num=purchaseService.insertwinepurchasesub(purchasesub);
		}
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "添加成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "添加失败！");
		}
		return mapjson;
	}
	
	/**
	 * 订单管理修改
	 * @param purchase
	 * @return
	 */
	@RequestMapping("/updatepurchasestate")
	@ResponseBody
	public Map<String, Object> updatepurchase(Purchase purchase){
		int num = purchaseService.updatepurchase(purchase);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "修改成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "修改失败！");

		}
		return mapjson;
		
	}
}
